import QtQuick 2.5
import QtQuick.Controls 1.4


//import Query 1.0
//import "../"
Rectangle {
    property TableView tableList: tableList
    property var clickRow
    property var roleNames
    property var tablelistName
    color: "white"
    signal doubleClick(int row)
    signal rightClick(int row, int col)
    signal leftClick(int row)
    signal gridWidthChange(int col)
    signal loadHeaderFinish
    property var titles

    //    Component {
    //        id: columnComponent
    //        TableViewColumn {
    //            width: 120
    //        }
    //    }

    //定义表头的委托
    Component {
        id: headerDele
        Rectangle {
            id: headerRect
            height: 30
            border.color: "lightgray"
            color: "black"

            Text {
                text: styleData.value
                anchors.centerIn: parent
                //                font.family: qsTr("微软雅黑")
                //                font.pixelSize: 12
                font.pixelSize: 14
                color: "white"
            }

            onWidthChanged: gridWidthChange(styleData.column)
        }
    }

    //定义行的委托
    Component {
        id: rowDele
        Rectangle {
            id: rect
            height: 24
            //            color: styleData.selected ? "gray" : "white"
            //            color: "transparent";
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton
                onClicked: {
                    //                    clicked();
                    //                    rowDele.parent.cliecked(styleData.row);
                    if (mouse.button == Qt.RightButton) {
                        //                        leftClick(styleData.row);
                        rightClick(styleData.row, styleData.column)
                        console.log("ListFrame -onClicked RightButton")
                    } else if (mouse.button == Qt.LeftButton) {
                        console.log("ListFrame -onClicked LeftButton " + styleData.selected)
                        leftClick(styleData.row)
                    }
                }
                onDoubleClicked: {
                    console.log("ListFrame -onDoubleClicked")
                    doubleClick(styleData.row)
                }

                onPressed: {
                    console.log("ListFrame -onClicked onPressed")
                }
            }
        }
    }

    // }
    TableView {
        id: tableList
        anchors.fill: parent
        anchors.leftMargin: 5
        anchors.rightMargin: 5
        backgroundVisible: false
        //        frameVisible: false
        //        itemDelegate: TableViewItem{} //添加自己喜欢的代理，这个就不用说了吧
        itemDelegate: Rectangle {
            //            width: 200
            //            height: 50
            //            color: "#6000FF00" //"green"
            color: styleData.selected ? "#FF3399FF" : "white"
            border.width: 1
            border.color: "gray"
            Text {
                id: cellT
                anchors.verticalCenter: parent !== null ? parent.verticalCenter : undefined
                anchors.left: parent !== null ? parent.left : undefined
                anchors.leftMargin: 3
                anchors.rightMargin: 2
                anchors.right: parent !== null ? parent.right : undefined
                color: styleData.textColor
                elide: styleData.elideMode
                text: styleData.value !== undefined ? styleData.value : ""
                horizontalAlignment: styleData.textAlignment
            }

            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.RightButton
                onClicked: {
                    //                    clicked();
                    //                    rowDele.parent.cliecked(styleData.row);
                    if (mouse.button == Qt.RightButton) {
                        //                        leftClick(styleData.row);
                        rightClick(styleData.row, styleData.column)
                        console.log("ListFrame Cell-onClicked RightButton")
                    } else if (mouse.button == Qt.LeftButton) {
                        console.log("ListFrame Cell-onClicked LeftButton " + styleData.selected)
                        leftClick(styleData.row)
                    }
                }
                onDoubleClicked: {
                    console.log("ListFrame Cell-onDoubleClicked")
                    doubleClick(styleData.row)
                }

                onPressed: {
                    console.log("ListFrame Cell-onClicked onPressed")
                }
            }
        }
        headerDelegate: headerDele //表头委托
        //            TableViewColumn{role:"title"; title: "Title"; width: 100}
        //            TableViewColumn{role:"author"; title: "Author"; width: 200}
        rowDelegate: rowDele //行委托

        onDoubleClicked: {
            console.log("ListFrame -onDoubleClicked")
            doubleClick(currentRow)
        }

        onClicked: {
            console.log("ListFrame -onClicked")
            clickRow = currentRow
            leftClick(currentRow)
        }

        Component.onCompleted: {
            //            console.log("updateColumn2Table TableView\'s C++ type -", tableList)
            //            console.log("onCompleted load ListFrame -", tablelistName);
            updateColumn(tablelistName)
        }

        //        onWidthChanged: console.log("onWidthChanged")
        //        onImplicitWidthChanged: console.log("onImplicitWidthChanged")
        //        onHeaderDelegateChanged: console.log("onHeaderDelegateChanged")
    }
    property var columnCount: 0

    function updateColumn(listName) {
        console.log("load ListFrame -", listName)
        if (listName == "")
            return
        //将现在的每列全部删除
        for (var i = 0; i < columnCount; i++) {
            tableList.removeColumn(0)
        }

        roleNames = colCfg.getRoleNames(listName)
        var widths = colCfg.getColumnWidths(listName)
        titles = colCfg.getTitles(listName)
        var aligns = colCfg.getColumnAligns(listName)
        var i, str, sAlign
        columnCount = roleNames.length

        var prefix = 'import QtQuick 2.0;import QtQuick.Controls 1.4;TableViewColumn {width: ' //创建TableViewColumn的代码

        //循环添加TableViewColumn
        for (i = 0; i < roleNames.length; i++) {
            sAlign = aligns[i] === 2 ? "Text.AlignHCenter" : (aligns[i] === 3 ? "Text.AlignRight" : "Text.AlignLeft")
            str = prefix + widths[i] + ";role:\"" + roleNames[i]
                    + "\";horizontalAlignment:" + sAlign + ";title:\"" + titles[i] + "\"}"
            tableList.addColumn(Qt.createQmlObject(str, tableList,
                                                   "dynamicSnippet1"))
        }

        //        sleep(500)
        loadHeaderFinish()
    }

    //    onGridWidthChange: {
    //        //        console.log("onGridWidthChange " + tableList.getColumn(col).role)
    //        listWidthChange(tableList.getColumn(col).role,
    //                        tableList.getColumn(col).width)
    //    }
    function setColumnWidth(role, width) {
        console.log("setColumnWidth " + role + " " + width)
        for (var i = 0; i < tableList.columnCount; ++i) {
            var column = tableList.getColumn(i)
            if (column.role === role)
                column.width = width
        }
    }
}
